﻿using System;
using EmbeddedSharp.Core;
using EmbeddedSharp.Scripting.Function.Mathematics;

namespace EmbeddedSharp.Scripting.Function.Statistics {
    public class MeanNode : FuncNode {

        public MeanNode()
            : base() {
            this.Precedence = 0;
            base.Value = "mean";
        }

        public override object Evaluate() {
            int index = 0;
            double[] d = new double[this.Parameters.Count];
            foreach (Node child in this.Parameters) {
                d[index++] = Convert.ToDouble(
                    child.Evaluate());
            }

            return StatisticsCore.Average(d).ToString();
        }
    }
}
